import Vue from 'vue'
import App from './App.vue'
import {
	Button,
	Radio,
	Container,
	Main,
	Header,
	Aside,
	Menu,
	Submenu,
	MenuItem,
	MenuItemGroup,
	Dropdown,
	DropdownMenu,
	DropdownItem,
	Row,
	Col,
	Card,
	Table,
	TableColumn,
	Breadcrumb,
	BreadcrumbItem,
	Tag,
	Form,
	FormItem,
	Input,
	select,
	Option,
	Switch,
	DatePicker,
	Dialog,
	Pagination,
	MessageBox,
	Message,
	Checkbox,
	Tree,
	Tabs,
	TabPane,
	// Loading,
	RadioGroup,
	Upload
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'
import router from '../router'
import store from './store'
import http from 'axios'
import '../api/mock.js'
import dateUtils from '../utils/dateUtils.js'
import VueContextMenu from 'vue-contextmenu'
// 富文本组件
import Editor from "@/components/Editor"

// 全局组件挂载
Vue.component('Editor', Editor)

Vue.use(Button);
Vue.use(Radio);
Vue.use(Container);
Vue.use(Main);
Vue.use(Header);
Vue.use(Aside);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(Dropdown);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(Row);
Vue.use(Col);
Vue.use(Card);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Breadcrumb);
Vue.use(BreadcrumbItem);
Vue.use(Tag);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Input);
Vue.use(select);
Vue.use(Option);
Vue.use(Switch);
Vue.use(DatePicker);
Vue.use(Dialog);
Vue.use(Pagination);
Vue.use(Checkbox);
Vue.use(Tree);
Vue.use(Tabs);
Vue.use(TabPane);
// Vue.use(Loading);
Vue.use(RadioGroup);
Vue.use(VueContextMenu);
Vue.use(Upload);

Vue.config.productionTip = false
Vue.prototype.$http = http;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$message = Message;

router.beforeEach((to, from, next) => {  //导航守卫
// alert(JSON.stringify(from.name)+'    '+JSON.stringify(to.name)+'    '+JSON.stringify(next))
	const token = localStorage.getItem("token")
	const expdate = localStorage.getItem("expdate")
	const lastdate_ = localStorage.getItem("lastdate")
	const lastDate = new Date(lastdate_)
	const curDate = new Date()
	let exp = false
	let span = dateUtils.DateDiff(curDate,lastDate)
	if(expdate && span>expdate){
		exp=true
	}
	if((!token || exp ) && to.name != 'login'){ //token为空，exp==true过期，目标页不是login
		next({name: 'login'})
	}
	else if(token && !exp && to.name === 'login'){  //token不为空，exp==false未过期，目标页是login
		next({name: 'home'})
	}
	else{
		next()
	}
})

new Vue({
  render: h => h(App),
  store,
  router,
  created(){
  	store.commit('addMenu', router)  //动态添加路由
  }
}).$mount('#app')
